/*
 * File: gmaps.js
 * Desc: The library use for Google Maps API
 * Author: Phuong Huynh (Pyttop@gmail.com)
 *
 * gmaps.ready(): trả về true nếu thư viện đã load từ google xong
 * gmaps.queue(function): thêm một hàm vào hàng đợi, sẽ được call khi load xong thư viện
 * gmaps.makeMap(id, options): tạo một canvas với id của phần tử DIV
 */

window.gmaps = window.gmaps || {};
gmaps.callbackName = '___gmapsInit';
gmaps.apiKey = 'AIzaSyAyEXgNgC32_IhPQqEyDohMOZou65Ee6gY';

(function () {

    var _load = false
        , _ready = false
        , _queue = []
        , maps = [];
    gmaps.ready = function () {
        return _ready;
    };

    gmaps.queue = function (func) {
        if (_ready) {
            func.call();
        } else {
            _queue.push(func);
        }
    };

    window[gmaps.callbackName] = function () {
        _ready = true;
        for (x in _queue) {
            _queue[x].call();
            delete _queue[x];
        }
    };

    gmaps.load = function (func) {
        if (func) {
            gmaps.queue(func);
        }
        if (!_load) {
            var apiUrl = "http://maps.googleapis.com/maps/api/js?key=" +
                    gmaps.apiKey + "&sensor=false&callback=" + gmaps.callbackName
                , script = doc.createElement('script')
                ;
            script.type = 'text/javascript';
            script.src = apiUrl;
            doc.getElementsByTagName('head')[0].appendChild(script);

            _load = true;
        }
    };

    gmaps.toLatLng = function (str) {
        var latLng = str.split(',');
        return new google.maps.LatLng(parseFloat(latLng[0]), parseFloat(latLng[1]));
    };

    gmaps.makeMap = function (id, options) {
        var map;
        gmaps.load(function () {
            if (options == undefined) {
                options = {
                    zoom: 8, center: new google.maps.LatLng(-25.363882, 131.044922), mapTypeId: 'roadmap'
                };
            }
            ;

            if (typeof options.center == 'string') {
                var latLng = options.center.split(',');
                options.center = new google.maps.LatLng(latLng[0], latLng[1]);
            }

            if (typeof options.mapTypeId == undefined) {
                options.mapTypeId = 'roadmap';
            }

            map = new google.maps.Map(doc.getElementById(id), options);
        });

        return map;
    };

})(window, document);



gmaps.load(function() {
    var myLatLng = new google.maps.LatLng(10.031440, 105.752009)
        , map = gmaps.makeMap('map-canvas', {
            zoom: 14
            , center: myLatLng
            , mapTypeId: 'roadmap'
        })
        , cross_nvlinh = new google.maps.LatLng(10.018287,105.762045)
        , marker1 = new google.maps.Marker({
            position: cross_nvlinh
            , map: map
            , icon: 'beachflag.png'
        })
        , bighall = new google.maps.LatLng(10.029211,105.769331)
        , marker2 = new google.maps.Marker({
            position: bighall
            , map: map
            , draggable: true
        })
        , directionDisplay = new google.maps.DirectionsRenderer()
        , directionService = new google.maps.DirectionsService()
        ;
    directionDisplay.setMap(map);

    $cal.disabled = false;

    function routed(response, status) {
        if (status == google.maps.DirectionsStatus.OK) {
            directionDisplay.setDirections(response);
            console.log(response.routes);
            var route = response.routes[0];
            console.log(route.legs[0].start_address + ' đến ' + route.legs[0].end_address);
            console.log('Tổng cộng: ' + route.legs[0].distance.text);
        }
    }
    $cal.onclick = function() {
        var selectedMode = $mode.value
            , request = {
                origin: marker1.getPosition()
                , destination: marker2.getPosition()
                , travelMode: google.maps.TravelMode[selectedMode]

            };

        directionService.route(request, routed);
    };

});